#include "XmlParserTest.h"

#include <QtTest/QtTest>

#include "TestSuite.h"
#include "XmlParser.h"
#include "BBClass.h"


void XmlMappingParserTest::instance()
{
	XmlParser* parser = XmlParser::instance();

	QVERIFY( parser != NULL );
}

void XmlMappingParserTest::instanceTwoTimes()
{
	XmlParser* parserFirst = XmlParser::instance();
	XmlParser* parserSecond = XmlParser::instance();

	QVERIFY( parserFirst == parserSecond );
}

void XmlMappingParserTest::getUserClasses()
{
	QString xmlMappingFileName( "TestXmlMappingParser.xml" );
	QFile xmlMappingFile( xmlMappingFileName );
	QString className( "TestClass" );
	QString attributeName( "testAttribute" );
	QString attributeType( "int" );
	//QString classExtension( "SuperClass" );
	
	if( !xmlMappingFile.open( QIODevice::WriteOnly) )
	{
		QVERIFY( false );
		return;
	}

	QString xmlCode = QString( "%1%2%3%4%5" )
		.arg("<blackbear-mapping>\n")
		.arg( "<class name=\"" + className + "\"" + ">\n" )
		.arg( "<attribute name=\"" + attributeName + "\" type=\"" + attributeType + "\"/>\n" )
		.arg( "</class>\n" )
		.arg("</blackbear-mapping>\n");

	QTextStream ts( &xmlMappingFile );
	ts << xmlCode;
	xmlMappingFile.close();

	XmlParser* parser = XmlParser::instance();
	
	QList<BBClass>* listClasses =  parser->getUserClasses( xmlMappingFileName );

	if( listClasses == NULL )
	{
		QVERIFY( false );
		return;
	}

	BBClass bbClass = listClasses->first();

	QCOMPARE( bbClass.getName(), className );
	QCOMPARE( bbClass.getAttributeType( attributeName ), attributeType );

	xmlMappingFile.remove();
}

REGISTER_TEST_CLASS( XmlMappingParserTest );